<ul class="ul-view">

</ul>
<script src="./javascripts/jQuery.js"></script>
<script>
    $(function () {



        $.ajax({
            url: "./json/commoditysec.json"
        })
            .then(function (res) {
                render(res.datas);
            })


        function render(list) {
            goods_data = list;
            $(".ul-view").html(

                list.map((item, index) => `
                    
                    <li class="li-view">
                        <div class="box-view">
                            <a href="./car.html" class="a-view" data-id=${index} >
                                <div class="img-view">
                                    <img class="lazy" src="${item.image}" alt="">
                                </div>
                                <div class="text-view">
                                    <div class="text-one"><p class="one-p">${item.p1}</p></div>
                                    <div class="text-two"><p>${item.p2}</p></div>
                                    <div class="text-two"><p>${item.p3}</p></div>
                                    <div class="text-thr">
                                        <p>
                                            <span>${item.price}</span>
                                            <span class="thr-sp">${item.p4}${item.cap}</span>
                                            </p></div>
                                </div>
                            </a>
                            <div class="collect"><img src="./img/收藏.png" alt=""></div>
                        </div>
                    </li>
                    `).join(""),

            )

        }


        $(".ul-view").on("click", ".a-view", function () {
            addCart($(this).attr("data-id"))
        });
        var goods_data = null;


        var cart = (function () {

            if (localStorage.getItem("cart")) {
                var data = null;
                try {
                    data = JSON.parse(localStorage.getItem("cart"));

                } catch (e) {
                    data = {};
                }
                return data;
            } else {
                return {};
            }
        })();



        function addCart(id) {
            console.log(goods_data);
            if (id in cart) {
                cart[id].count++;
            } else {
                // 给数据添加上数量信息; 
                goods_data[id].count = 1;
                cart[id] = goods_data[id]
            }
            localStorage.setItem("cart", JSON.stringify(cart))
        }

    })



</script>
<script>
      
    $(function(){
          $(".lazy").lazyload();
    });

</script>